package org.yunshang.supplier.service.impl;

import org.yunshang.common.core.utils.MapstructUtils;
import org.yunshang.common.core.utils.StringUtils;
import org.yunshang.common.mybatis.core.page.TableDataInfo;
import org.yunshang.common.mybatis.core.page.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.yunshang.supplier.domain.bo.TSupplierBo;
import org.yunshang.supplier.domain.vo.TSupplierVo;
import org.yunshang.supplier.domain.TSupplier;
import org.yunshang.supplier.mapper.TSupplierMapper;
import org.yunshang.supplier.service.ITSupplierService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 供应商信息Service业务层处理
 *
 * @author Lion Li
 * @date 2024-03-29
 */
@RequiredArgsConstructor
@Service
public class TSupplierServiceImpl implements ITSupplierService {

    private final TSupplierMapper baseMapper;

    /**
     * 查询供应商信息
     */
    @Override
    public TSupplierVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询供应商信息列表
     */
    @Override
    public TableDataInfo<TSupplierVo> queryPageList(TSupplierBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TSupplier> lqw = buildQueryWrapper(bo);
        Page<TSupplierVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询供应商信息列表
     */
    @Override
    public List<TSupplierVo> queryList(TSupplierBo bo) {
        LambdaQueryWrapper<TSupplier> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TSupplier> buildQueryWrapper(TSupplierBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TSupplier> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getName()), TSupplier::getName, bo.getName());
        lqw.like(StringUtils.isNotBlank(bo.getBigName()), TSupplier::getBigName, bo.getBigName());
        lqw.eq(StringUtils.isNotBlank(bo.getPhone()), TSupplier::getPhone, bo.getPhone());
        lqw.eq(StringUtils.isNotBlank(bo.getUrl()), TSupplier::getUrl, bo.getUrl());
        lqw.eq(StringUtils.isNotBlank(bo.getPeople()), TSupplier::getPeople, bo.getPeople());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), TSupplier::getStatus, bo.getStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getType()), TSupplier::getType, bo.getType());
        lqw.eq(StringUtils.isNotBlank(bo.getPay()), TSupplier::getPay, bo.getPay());
        return lqw;
    }

    /**
     * 新增供应商信息
     */
    @Override
    public Boolean insertByBo(TSupplierBo bo) {
        TSupplier add = MapstructUtils.convert(bo, TSupplier.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改供应商信息
     */
    @Override
    public Boolean updateByBo(TSupplierBo bo) {
        TSupplier update = MapstructUtils.convert(bo, TSupplier.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TSupplier entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除供应商信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
